package com.wang;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @ClassName MyPreDate
 * @Description
 * @Author wangyang
 * @Date 2022/9/14 14:46
 * @Version 1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class MyPreDate {
    private int year;
    private int month;
    private int day;

    /**
     * @param year
     * @param month
     * @param day
     * @Description:validateDate方法校验给定的日期是否合法
     * @Author: wangyang
     * @Date: 2022/9/14 14:50
     **/
    public boolean validateDate(int year, int month, int day) {
        boolean flag = false;
        //闰年，2月29天
        if (isMonth(month) && day > 0 ) {
            if (month == 2) {
                if (isLeapYear(year) && day <= 29) {
                    flag = true;
                } else {
                    if (day <= 28) {
                        flag = true;
                    }
                }
            } else {
                if (isBigMonth(month) && day <= 31) {
                    flag = true;
                } else {
                    if (day <= 30) {
                        flag = true;
                    }
                }
            }

        }
        return flag;
    }


    /**
     * @param year
     * @Description:判断是否是闰年
     * @Author: wangyang
     * @Date: 2022/9/14 15:00
     **/
    public boolean isLeapYear(int year) {
        boolean flag = false;
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            flag = true;
        }
        return flag;
    }


    /**
     * @Description:preDate方法对给定的日期输出该日期的前一天的日期
     * @Author: wangyang
     * @Date: 2022/9/14 15:03
     * @return: boolean
     **/
    public String preDate(int year, int month, int day) throws ParseException {
//        if (!validateDate(year, month, day)) {
//            return "日期不合法!";
//        }
//        String preDate = "";
//        String nowDate = year + "-" + month + "-" + day;
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//        Date date = formatter.parse(nowDate);
//        Calendar calendar = new GregorianCalendar();
//        calendar.setTime(date);
//        calendar.add(Calendar.DATE, -1);//把日期往后增加一天.整数往后推,负数往前移动
//        date = calendar.getTime(); //这个时间就是日期往后推一天的结果
//        preDate = formatter.format(date);
//        return preDate;
        if (!validateDate(year, month, day)) {
            return "日期不合法";
        }
        if (day == 1) {
            if (month == 1) {
                year = year - 1;
                month = 12;
                day = 31;
            } else {
                if (month == 3) {
                    if (isLeapYear(year)) {
                        day = 29;
                    } else {
                        day = 28;
                    }
                } else {
                    if (isBigMonth(month - 1)) {
                        day = 31;
                    } else {
                        day = 30;
                    }
                }
                month = month - 1;
            }
        } else {
            day = day - 1;
        }

        this.year = year;
        this.month = month;
        this.day = day;
        return this.year+"-"+this.month+"-"+this.day;
    }


    /**
     * @param month
     * @Description:isBigMonth判定给定的月份是31天还是30天
     * @Author: wangyang
     * @Date: 2022/9/14 15:05
     **/
    public boolean isBigMonth(int month) {
        if (!isMonth(month)) {//月份不合法
            throw new RuntimeException("月份不合法");
        }
        final Object[] arr = {1, 3, 5, 7, 8, 10, 12};
        if (Arrays.asList(arr).contains(month)) {
            return true;
        }
        return false;
    }

    /**
     * @param month
     * @param month
     * @Description:判断月份是否合格
     * @Author: wangyang
     * @Date: 2022/9/14 15:31
     **/
    private boolean isMonth(int month) {
        if (month > 0 && month < 13) {
            return true;
        }
        return false;
    }
}
